﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using BattleEngine2D;
using MarvinsArena.Core;
using BattleLogic;

namespace Game
{
	public class MarvinsArenaGame : Microsoft.Xna.Framework.Game
	{
		private GraphicsDeviceManager graphics;
		private SpriteBatch spriteBatch;
		private Logic logic;
		private BattleEngine battleEngine;		

		public MarvinsArenaGame(Tournament tournament)
		{
			this.graphics = new GraphicsDeviceManager(this);
			this.Content.RootDirectory = "Content";

			graphics.PreferredBackBufferWidth = 1024;//Properties.Settings.Default.PreferredBackBufferWidth;
			graphics.PreferredBackBufferHeight = 768;//Properties.Settings.Default.PreferredBackBufferHeight;
			graphics.IsFullScreen = false;//Properties.Settings.Default.IsFullScreen;
			graphics.ApplyChanges();

			logic = new Logic(tournament);
			battleEngine = new BattleEngine(this.graphics, this.Content, tournament);

		}
		
		protected override void Initialize()
		{
			this.spriteBatch = new SpriteBatch(this.GraphicsDevice);
            
            logic.InitializeLogic();
			battleEngine.Initialize(this.spriteBatch);
			base.Initialize();
		}

		protected override void LoadContent()
		{
            battleEngine.LoadContent();
			
			base.LoadContent();
		}

		protected override void Update(GameTime gameTime)
		{
			logic.Update(gameTime);
			
			((IGame)battleEngine).camera = ((IGame)logic).camera;
			((IGame)battleEngine).hudMessages = ((IGame)logic).hudMessages;
			((IGame)battleEngine).map = ((IGame)logic).map;
			((IGame)battleEngine).projectiles = ((IGame)logic).projectiles;
			((IGame)battleEngine).robots = ((IGame)logic).robots;

			base.Update(gameTime);
		}

		protected override void Draw(GameTime gameTime)
		{
            GraphicsDevice.Clear(Color.Blue);

            this.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);

			battleEngine.Draw(gameTime);

            this.spriteBatch.End();

            base.Draw(gameTime);
		}

		

		protected override void UnloadContent()
		{
			base.UnloadContent();
		}
	}
}
